package Servlet;

import Dao.studentimply;
import Pojo.Page;
import Pojo.Student;
import Service.Service;
import org.junit.Test;

import java.util.List;

public class Serviceimply implements Service {
    Dao.studentDao studentDao=new studentimply();

    @Override
    public void add(Student student) {
          studentDao.add(student);
    }

    @Override
    public boolean existBysnameAndpwdPwd(String sname,String pwd) {
        if(studentDao.selectBysnameAndpwd(sname,pwd)==null){
            return  false;
        }
        return  true;
    }

    @Override
    public List<Student> queryList() {
        List<Student> list = studentDao.selectForlist();
        return list;
    }


    @Override
    public Student selectOne(Integer sno) {
       return studentDao.selectByno(sno);
    }

    @Override
    public void delete(String sno) {
        studentDao.deleteByid(sno);
    }

    @Override
    public void update(Student student) {
        studentDao.update(student);
    }

    @Override
    public Student login(Student student) {
       return studentDao.queryUserByUsernameAndPassword(student.getSname(),student.getPwd());
    }

    @Override
    public Page<Student> page(int pageNo, int pageSize) {
        Page<Student> page = new Page<>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置当前页的显示数量
        page.setPageSize(pageSize);
        //设置总记录数
        Integer PageTotalCount=studentDao.queryPageTotalCount();
        page.setPageTotalCount(PageTotalCount);
        //设置总页码
        int Total=PageTotalCount/pageSize;

        if(PageTotalCount%pageSize>0){
            Total+=1;
        }
        page.setPageTotal(Total);
        //        数据边界的检查
        if(pageNo<1){
            pageNo=1;
        }
        if(pageNo>Total){
            pageNo=Total;
        }
        //当前页码数据
        int begin=(page.getPageNo()-1)*pageSize;
        List<Student> item=studentDao.queryPageItem(begin,pageSize);
        page.setItems(item);
        return page;
    }
}
